package cn.zhuama.org.service.impl;

import cn.zhuama.basic.exception.BusinessException;
import cn.zhuama.basic.sevice.impl.BaseServiceImpl;
import cn.zhuama.org.domain.Employee;
import cn.zhuama.org.domain.Shop;
import cn.zhuama.org.domain.ShopAuditLog;
import cn.zhuama.org.domain.ShopDto;
import cn.zhuama.org.mapper.EmployeeMapper;
import cn.zhuama.org.mapper.ShopAuditLogMapper;
import cn.zhuama.org.mapper.ShopMapper;
import cn.zhuama.org.service.IShopService;
//import cn.zhuama.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.Date;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;

    @Autowired
    private JavaMailSender javaMailSender;


    @Override
    public void settlement(Shop shop) {
        //1.参数校验
        if(shop.getName()==null || shop.getName().length()==0){
            throw new BusinessException("店铺名字不能为空");
        }
        //2.逻辑判断
        if (shopMapper.selectByNameAndAddress(shop)!=null){
            throw new BusinessException("请勿重复注册店铺");
        }
        //3.业务实现
        shop.setState(Shop.WAITING_FOR_REVIEW);
        shopMapper.save(shop);
        //4.保存管理员
        Employee admin = shop.getAdmin();
        admin.setState(Employee.enable);
        admin.setShop_id(shop.getId());
        employeeMapper.saveRegis(admin);
        //5.更新商铺的管理员ID
        shop.setAdminId(admin.getId());
        shopMapper.update(shop);
    }

    @Override
    public void reject(ShopAuditLog log) {
        //1.记录日志
        log.setState(Shop.REJECT);
        //模拟个管理员
        Long guanli = 1L;
        log.setAuditId(guanli);
        shopAuditLogMapper.save(log);
        //获取店铺信息
        Shop shop = shopMapper.queryOne(log.getShopId());
        shop.setState(Shop.REJECT);
        update(shop);
    }

    @Override
    public void pass(ShopAuditLog log) throws MessagingException {
        //1.记录日志
        log.setState(Shop.APPROVED_AND_AWAITING_ACTIVATION);
        //模拟个管理员
        Long guanli = 1L;
        log.setAuditId(guanli);
        shopAuditLogMapper.save(log);
        //获取店铺信息
        Shop shop = shopMapper.queryOne(log.getShopId());
        shop.setState(Shop.APPROVED_AND_AWAITING_ACTIVATION);
        update(shop);
        //发送邮件 用于激活
        //创建复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        helper.setFrom("273520113@qq.com");
        helper.setSubject("店铺激活邮件");
        helper.setText("<h1>你的店铺已经注册!!!</h1><img src='http:/39.105.218.224"+shop.getLogo()+"'><a href='http://localhost:8080/shop/active/"+shop.getId()+"'>点击该链接激活</a>",true);
        //添加附件
        helper.addAttachment("zhengjian.jpeg",new File("C:\\Users\\asus\\Pictures\\zhengjian.jpeg"));
//      helper.addAttachment("g1.gif", new File("C:\\Users\\Administrator\\Pictures\\g1.gif"));
        //收件人
        helper.setTo("823754469@qq.com");
        javaMailSender.send(mimeMessage);
    }

    @Override
    public boolean run(Long id) {
        Shop shop = shopMapper.queryOne(id);
        if(shop.getId()!=null){
            shop.setState(Shop.ACTIVATION_SUCCESS);
            update(shop);
            return true;
        }else {
            return false;
        }

    }

    @Override
    public List<ShopDto> getCountByState() {
        return shopMapper.getCountByState();
    }
}
